import {createRouter, createWebHistory} from 'vue-router'
import {useRouterStore} from "@/stores/router.js";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import("@/views/login/index.vue"),
            meta: {
                title: "登录"
            }
        },
        {
            path: '/admin',
            redirect: "/order",
            name: 'layout',
            component: () => import("@/views/layout-admin/index.vue"),
            children: [
                {
                    path: "/order",
                    name: "order",
                    component: () => import("@/views/order/index.vue"),
                    meta: {
                        title: "订单管理",
                        authorities: "ADMIN,MERCHANT",
                        isKeepAlive: true
                    },
                },
                {
                    path: "/material",
                    name: "material",
                    component: () => import("@/views/material/index.vue"),
                    meta: {
                        title: "材料管理",
                        authorities: "ADMIN,MERCHANT",
                        isKeepAlive: true
                    },
                },
                {
                    path: "/user",
                    name: "user",
                    component: () => import("@/views/user/index.vue"),
                    meta: {
                        title: "用户管理",
                        authorities: "ADMIN",
                        isKeepAlive: true
                    },
                },
                {
                    path: "/model",
                    name: "model",
                    component: () => import("@/views/model/index.vue"),
                    meta: {
                        title: "模型管理",
                        authorities: "ADMIN,MERCHANT",
                        isKeepAlive: true
                    },
                },
                {
                    path: "/orderDetails",
                    name: "orderDetails",
                    component: () => import("@/views/order-details/index.vue"),
                    meta: {
                        title: "订单明细管理",
                        authorities: "ADMIN,MERCHANT",
                        isKeepAlive: true
                    },
                }
            ]
        },

        {
            path: '/',
            redirect: "/index",
            component: () => import("@/views/layout/index.vue"),
            meta: {
                title: "首页",
                isKeepAlive: false
            },
            children: [
                {
                    path: 'index',
                    name: "index",
                    component: () => import("@/views/home/index.vue"),
                    meta: {
                        title: "首页",
                        isKeepAlive: false
                    }
                },
                {
                    path: 'quotation',
                    name: "quotation",
                    component: () => import("@/views/quotation/index.vue"),
                    meta: {
                        isKeepAlive: false
                    },
                    children: [
                        {
                            path: 'print',
                            name: "print",
                            component: () => import("@/views/quotation/components/3Dprint.vue"),
                            meta: {
                                title: "3D加工",
                                isKeepAlive: true
                            }
                        }
                    ]
                },
                {
                    path: 'myorder',
                    name: "myorder",
                    component: () => import("@/views/my-order/index.vue"),
                    meta: {
                        title: "我的订单",
                        isKeepAlive: false
                    }
                },
                {
                    path: "/details/:id",
                    name: "details",
                    component: () => import("@/views/details/index.vue"),
                    props: true,
                    meta: {
                        title: "订单详细",
                        isKeepAlive: true
                    },
                },
            ]
        },
        {
            path: '/muterorder',
            name: "muterorder",
            component: () => import("@/views/muteorder/index.vue"),
            meta: {
                isKeepAlive: true
            },
            children: [
                {
                    path: 'create',
                    name: "create",
                    component: () => import("@/views/muteorder/create/index.vue"),
                    meta: {
                        title: "创建订单",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'appquotation',
                    name: "appquotation",
                    props: true,
                    component: () => import("@/views/muteorder/app-quotation/index.vue"),
                    meta: {
                        title: "申请报价",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'finishquotation',
                    name: "finishquotation",
                    props: true,
                    component: () => import("@/views/muteorder/finish-quotation/index.vue"),
                    meta: {
                        title: "报价",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'pay',
                    name: "pay",
                    props: true,
                    component: () => import("@/views/muteorder/pay/index.vue"),
                    meta: {
                        title: "支付",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'printing',
                    name: "printing",
                    props: true,
                    component: () => import("@/views/muteorder/printing/index.vue"),
                    meta: {
                        title: "打印中",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'finishprint',
                    name: "finishprint",
                    props: true,
                    component: () => import("@/views/muteorder/finish-print/index.vue"),
                    meta: {
                        title: "打印完成",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'finishorder',
                    name: "finishorder",
                    props: true,
                    component: () => import("@/views/muteorder/finish-order/index.vue"),
                    meta: {
                        title: "订单完成",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'finish',
                    name: "finish",
                    props: true,
                    component: () => import("@/views/muteorder/finish/index.vue"),
                    meta: {
                        title: "订单完成",
                        isKeepAlive: true
                    }
                },
                {
                    path: 'cancel',
                    name: "cancel",
                    props: true,
                    component: () => import("@/views/muteorder/cancel/index.vue"),
                    meta: {
                        title: "取消订单",
                        isKeepAlive: true
                    }
                }
            ]
        }
    ]
})

//为面包屑导航带有参数的匹配路径matched数组中的路由信息添加全路径
router.beforeEach((to, from) => {
    // const userStore = useUserStore();
    // if(!userStore.account.credentialsNonExpired) {
    //     ElMessageBox.confirm("您未登录，是否要去登录", "提示", {
    //         confirmButtonText: "确定",
    //         type: "warning",
    //     }).then(() => {
    //         userStore.reset()
    //         window.location.href = "/login"
    //     }).catch(() => {
    //         return false
    //     })
    // }
})
router.afterEach((to, from) => {
    document.title = to.meta.title
    const routerStore = useRouterStore();
    if (to.meta.isKeepAlive) {
        !routerStore.include.includes(to.name) && routerStore.include.push(to.name);
    }
})
export default router
